package com.syh.jn.scm.service.impl.operation.system;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.spring.SpringContextUtils;
import cn.redscarf.commons.wrapper.Wrapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.operation.entity.system.OperationAccount;
import com.syh.jn.scm.domain.operation.entity.system.OperationRole;
import com.syh.jn.scm.domain.operation.repository.system.OperationAccountRepository;
import com.syh.jn.scm.domain.operation.repository.system.OperationRoleRepository;
import com.syh.jn.scm.dto.operation.system.menu.OperaMenuVo;
import com.syh.jn.scm.dto.operation.system.permission.PermissionVo;
import com.syh.jn.scm.dto.operation.system.role.OperationRoleMto;
import com.syh.jn.scm.dto.operation.system.role.OperationRoleSto;
import com.syh.jn.scm.dto.operation.system.role.OperationRoleVo;
import com.syh.jn.scm.exception.BusinessException;
import com.syh.jn.scm.service.api.operation.system.OperationRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class OperationRoleServiceImpl extends AbstractServiceImpl<OperationRoleSto, OperationRoleMto, OperationRoleVo, OperationRole, Long> implements OperationRoleService {

  @Autowired
  private OperationRoleRepository operationRoleRepository;

  @Override
  protected AggregateRepository<OperationRole, Long> getAggregateRepository() {
    return this.operationRoleRepository;
  }

  /**
   * 更新角色菜单
   *
   * @param roleId
   * @param menuIds
   */
  @Override
  public void updateRoleMenus(Long roleId, List<Long> menuIds) {
    OperationRole role = operationRoleRepository.getById(roleId);
    ExtAssert.notNull(role, "对象不存在");
    role.updateMenus(menuIds);
    operationRoleRepository.update(role, true);
  }

  @Override
  public Long save(OperationRoleSto sto) {
    OperationRole operationRole = new OperationRole(sto);
    return operationRoleRepository.add(operationRole);
  }

  @Override
  public void update(OperationRoleMto operationRoleMto) {
    ExtAssert.notNull(operationRoleMto, "对象为空");
    ExtAssert.notNull(operationRoleMto.getId(), "id为空");
    OperationRole operationRole = operationRoleRepository.getById(operationRoleMto.getId());
    operationRole.modify(operationRoleMto);
    operationRoleRepository.update(operationRole);
  }

  @Override
  public void delete(Long id) {
    OperationRole operationRole = operationRoleRepository.getById(id);
    if (operationRole == null) {
      return;
    }
    operationRole.destroy();
    operationRoleRepository.removeById(id);
  }

  @Override
  public OperationRoleVo getRole(Long accountId) {
    ExtAssert.notNull(accountId, "id为空");
    OperationAccountRepository downAccountRepository = (OperationAccountRepository) SpringContextUtils.getBean(OperationAccountRepository.class);
    OperationAccount operationAccount = downAccountRepository.getById(accountId);
    if (operationAccount == null) {
      throw new BusinessException("该账号无数据存在");
    }
    OperationRole role = operationRoleRepository.getById(operationAccount.getRoleId());
    if (role == null) {
      return null;
    }
    OperationRoleVo roleVo = new OperationRoleVo();
    ExtBeanUtils.copyProperties(role, roleVo);
    return roleVo;
  }

  @Override
  public List<PermissionVo> getPermissionByRoleId(Long roleId) {
    return operationRoleRepository.getPermissionByRoleId(roleId);
  }

  /**
   * 获取角色菜单
   *
   * @param roleId
   */
  @Override
  public List<OperaMenuVo> getMenusByRoleId(Long roleId) {
    return this.operationRoleRepository.getMenusByRoleId(roleId);
  }

  /**
   * 根据查询容器 获取角色菜单
   *
   * @param wrapper
   * @return
   */
  @Override
  public List<OperaMenuVo> getRoleMenusByWrapper(Wrapper wrapper) {
    return operationRoleRepository.getRoleMenusByWrapper(wrapper);
  }

  /**
   * 获取角色菜单
   *
   * @param roleId
   */
  @Override
  public List<OperaMenuVo> getMenusButtonByRoleId(Long roleId) {
    return null;
  }

  @Override
  public List<OperationRoleVo> getAllRolesWithPermission() {
    List<OperationRoleVo> roles = operationRoleRepository.getAllRolesWithPermission();
    return roles;
  }


  @Override
  public List<OperationRoleVo> getRolesByPermissionId(Long perId) {
    return operationRoleRepository.getRolesByPermissionId(perId).stream().map(role -> {
      OperationRoleVo roleVo = new OperationRoleVo();
      ExtBeanUtils.copyProperties(role, roleVo);
      return roleVo;
    }).collect(Collectors.toList());
  }
}
